package com.dnzx.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.dnzx.framework.orm.mybatis.MyBatisBaseServiceImpl;
import org.dnzx.framework.orm.mybatis.MybatisBaseDao;
import org.dnzx.framework.page.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dnzx.dao.IMenuDao;
import com.dnzx.model.system.MenuBO;
import com.dnzx.service.IMenuService;
import com.dnzx.service.IRoleMenuService;


/**
 * <p>系统后台目录serivce实现类 </p>
 *
 * @author Steven
 * @version V1.0
 * @Package com.dnzx.service.impl
 * 
 * @date 2017/9/15 11:11
 */
@Service
public class MenuServiceImpl extends MyBatisBaseServiceImpl<MenuBO,Long> implements IMenuService {

	@Resource
    private IMenuDao menuDao;
	@Autowired
    private IRoleMenuService roleMenuService;
    
    @Override
    public MybatisBaseDao<MenuBO, Long> getBaseDAO() {
        return this.menuDao;
    }

    @Override
    public void save(MenuBO menuBO) {
        this.menuDao.save(menuBO);
    }
    
    @Override
    public void delete(Long id) {
        this.menuDao.delete(id);
    }

    @Override
    public void update(MenuBO menuBO) {
    }
    
    @Override
    public MenuBO get(Long id) {
    	MenuBO menuBO = this.menuDao.get(id);
    	return menuBO;
    }
    
    @Override
    public List<MenuBO> queryList(MenuBO menuBO) {
    	return this.menuDao.queryList(menuBO);
    }
    
    @Override
    public List<MenuBO> queryListByRole(MenuBO menuBO) {
    	return this.menuDao.queryListByRole(menuBO);
    }

    @Override
    public PageResult<List<MenuBO>> queryPage(MenuBO menuBO, PageResult<List<MenuBO>> pageResult) {
        int totalRows = this.menuDao.queryCount(menuBO);
        pageResult.setTotalRows(totalRows);
        pageResult.setCurrentPage(menuBO.getCurrentPage());
        pageResult.setPageSize(menuBO.getPageSize());
		List<MenuBO> records = this.menuDao.queryPage(pageResult.getPageSize(), pageResult.getStartRow(), menuBO);
		pageResult.setT(records);
		return pageResult;
    }
    
    @Override
	public MenuBO queryTree(Long roleId) {
    	Map<Long, Long> map = this.roleMenuService.queryMap(roleId);
		MenuBO menuBO = new MenuBO();
		menuBO.setParentIdQuery(0L);
		this.querySubList(menuBO, map);// 引用传值
		return menuBO;
	}
    
    @Override
	public MenuBO queryTreeByRole(Long roleId) {
    	MenuBO menuBO = new MenuBO();
		menuBO.setParentIdQuery(0L);
		menuBO.setRoleId(roleId);
		this.querySubListByRole(menuBO);// 引用传值
		return menuBO;
	}
	    
    /*
	 * 获取下级区域列表 - 递归(引用传值)
	 */
	private void querySubList(MenuBO menuBO, Map<Long, Long> map) {
		List<MenuBO> subMenuBOList = this.menuDao.queryList(menuBO);// 查询下级
		if (subMenuBOList != null && subMenuBOList.size() > 0) {
			menuBO.setSubMenuBOList(subMenuBOList);
			for (MenuBO subMenuBO : subMenuBOList) {
				if (!map.containsKey(subMenuBO.getId())) {// 不包含这个菜单,设定为未选中
					subMenuBO.setIsCheck("0"); 
				} else {
					subMenuBO.setIsCheck("1"); 
				}
				subMenuBO.setParentIdQuery(subMenuBO.getId());
				querySubList(subMenuBO, map);
			}
		}
	}
	
	/*
	 * 获取下级区域列表 - 递归(引用传值)
	 */
	private void querySubListByRole(MenuBO menuBO) {
		List<MenuBO> subMenuBOList = this.menuDao.queryListByRole(menuBO);// 查询下级
		if (subMenuBOList != null && subMenuBOList.size() > 0) {
			menuBO.setSubMenuBOList(subMenuBOList);
			for (MenuBO subMenuBO : subMenuBOList) {
				subMenuBO.setParentIdQuery(subMenuBO.getId());
				subMenuBO.setRoleId(menuBO.getRoleId());
				querySubListByRole(subMenuBO);
			}
		}
	}
    
}
